import pygame.sprite

from Action.Shoot import ShootDir, DirShootByAImage
from Action.Walk import DirWalkByAImage, WalkDir
from Actors.Bullet import Shoot_Bullet
from Actors.Chinese import ChineseSoldier


class LeadActor(ChineseSoldier):
    def __init__(self, x, y):
        super(LeadActor, self).__init__(x, y)
        self.dir = WalkDir.down
        self.image_width = 50
        self.image_height = 74
        self.pos_x = x
        self.pos_y = y
        # 主角碰撞范围
        self.rect = pygame.Rect(self.pos_x + 5, self.pos_y + 5, 35, 60)
        # 子弹初始化
        self.bullet = Shoot_Bullet(50, 300, "Resources/images/welcome/%d-1.png", 9)
        # 初始化人物外貌
        self.little_boy = DirWalkByAImage("./Resources/images/chinese/militia/boy1.png", self.image_width,
                                          self.image_height, True)
        self.army_boy = DirWalkByAImage("./Resources/images/chinese/red_army/boy1.png", self.image_width,
                                        self.image_height, True)
        self.army_man = DirWalkByAImage("./Resources/images/chinese/red_army/red_army2.png", self.image_width,
                                        self.image_height, True)
        # 初始化人物方向
        self.little_boy.set_dir(self.dir)
        self.army_boy.set_dir(self.dir)
        self.army_man.set_dir(self.dir)
        # 当前状态
        self.current_action = self.little_boy

    def draw(self, surface, x: int, y: int):

        if self.shoot_action.shoot_dir == ShootDir.not_shoot:
            current_image = self.current_action.get_image()
            surface.blit(current_image, (self.pos_x - x, self.pos_y - y))
            pygame.draw.rect(surface, pygame.Color(0, 255, 0),
                             (self.pos_x - x + 5, self.pos_y - y + 5,
                              35, 70), 1)
        else:
            current_image = self.shoot_action.get_image()
            surface.blit(current_image, (self.pos_x - x - 26, self.pos_y - y - 20))

    def run(self, down_flag, key_list, obstacle_group: pygame.sprite.Group, japanese_group: pygame.sprite.Group):

        off_x = 0
        off_y = 0
        # 行走方向
        if key_list[pygame.K_DOWN]:
            self.dir = WalkDir.down
            self.shoot_action.set_dir(ShootDir.not_shoot)
            off_y = 10

        elif key_list[pygame.K_UP]:
            self.dir = WalkDir.up
            self.shoot_action.set_dir(ShootDir.not_shoot)
            off_y = -10

        elif key_list[pygame.K_LEFT]:
            self.dir = WalkDir.left
            self.shoot_action.set_dir(ShootDir.not_shoot)
            off_x = -10

        elif key_list[pygame.K_RIGHT]:
            self.dir = WalkDir.right
            self.shoot_action.set_dir(ShootDir.not_shoot)
            off_x = 10

        # 射击方向
        if down_flag:
            if key_list[pygame.K_q]:
                self.shoot_action.set_dir(ShootDir.left_up)
            elif key_list[pygame.K_a]:
                self.shoot_action.set_dir(ShootDir.left_down)
            elif key_list[pygame.K_e]:
                self.shoot_action.set_dir(ShootDir.right_up)
            elif key_list[pygame.K_d]:
                self.shoot_action.set_dir(ShootDir.right_down)

        self.little_boy.set_dir(self.dir)
        self.army_boy.set_dir(self.dir)
        self.army_man.set_dir(self.dir)

        # 碰撞的检测
        temp_rect = self.rect
        self.rect = self.rect.move(off_x, off_y)
        collide_list1 = pygame.sprite.spritecollide(self, obstacle_group, False)
        collide_list2 = pygame.sprite.spritecollide(self, japanese_group, False)
        if len(collide_list1) > 0 or len(collide_list2) > 0:
            self.rect = temp_rect
        else:
            self.pos_y += off_y
            self.pos_x += off_x

    def set_status(self, status: str):
        if status == "boy":
            self.current_action = self.little_boy
        else:
            self.current_action = self.army_boy
